package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.GetIhomeBean;
import cn.bocweb.spinning.model.Bean.IndexSliderBean;
import cn.bocweb.spinning.model.Bean.QaListBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.MainAdapterInterface;
import cn.bocweb.spinning.view.activity.CountActivity;
import cn.bocweb.spinning.view.activity.ExerciseActivity;
import cn.bocweb.spinning.view.activity.InfoDetailActivity;
import cn.bocweb.spinning.view.activity.LoginAcivity;
import cn.bocweb.spinning.view.activity.MyTestCollectActivity;
import cn.bocweb.spinning.view.activity.MyTestNoteActivity;
import cn.bocweb.spinning.view.activity.QuestionListActivity;
import cn.bocweb.spinning.view.activity.RankActivity;
import cn.bocweb.spinning.view.activity.ReadyTestActivity;
import cn.bocweb.spinning.view.activity.TestRuleActivity;
import cn.bocweb.spinning.view.activity.TrainActivity;
import cn.bocweb.spinning.view.activity.WebActivity;
import cn.bocweb.spinning.view.common.SwipeListView;

/**
 * Created by chenchangxing on 16/5/18.
 */
public class MainRecycleAdapter extends RecyclerView.Adapter {

    List<String> bannerUri;
    List<String> bannerTitle;
    List<ImageView> dotList;
    List<IndexSliderBean.DataBean> mDataBeanList;
    Context mContext;
    LinearLayout.LayoutParams selectParams;
    LinearLayout.LayoutParams unselect;
    ImageView dots;
    MainAdapterInterface mMainAdapterInterface;
    String userid;
    ViewHolder0 holder0;
    FragmentManager manager;
    List<GetIhomeBean.DataEntity> getIHome;
    List<QaListBean.DataBean> mList;
    public MainRecycleAdapter(List<String> bannerUri,
                              List<IndexSliderBean.DataBean> mDataBeanList,
                              FragmentManager manager) {
        this.bannerUri = bannerUri;
        this.mDataBeanList = mDataBeanList;
        this.manager = manager;
        getIHome = new ArrayList<>();
        mList = new ArrayList<>();
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    private void initUserid() {
        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
        dataEntity.readInfoFromLocal(mContext);
        userid = dataEntity.getUserid();
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();

        switch (viewType) {
            case 0: {
                //banner
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_0, parent, false);
                return new ViewHolder0(view);
            }
            case 1: {
                //考试模块
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_1, parent, false);
                return new ViewHolder1(view);
            }
            case 2: {
//                //技术问答
//                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_3, parent, false);
//                return new ViewHolder3(view);
                //论坛
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_forum, parent, false);
                return new ViewHolderForum(view);
            }
            case 3: {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_info, parent, false);
                return new ViewHolderInfo(view);
            }
            case 4: {
                //分享
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_main_share, parent, false);
                return new ViewHolder2(view);
            }
            default: {
                return null;
            }
        }
    }

    public void setGetIHome(List<GetIhomeBean.DataEntity> getIHome) {
        this.getIHome.clear();
        this.getIHome.addAll(getIHome);
    }

    public void setList(List<QaListBean.DataBean> list) {
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case 0: {
                holder0 = (ViewHolder0) holder;


                holder0.mMainBanner.setPages(new CBViewHolderCreator() {
                    @Override
                    public BannerAdapter createHolder() {
                        return new BannerAdapter();
                    }
                }, bannerUri);


                holder0.mMainBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {
                        initSelectDot(position);
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });

                holder0.mMainBanner.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        if (!mDataBeanList.get(position).getUrl().isEmpty()) {

                            Intent intent = new Intent(mContext, TestRuleActivity.class);
                            Bundle bundle = new Bundle();
                            UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                            dataEntity.readInfoFromLocal(mContext);
                            Boolean isLogin = !(dataEntity.getUserid() == null || dataEntity.getUserid().isEmpty());

                            if (mDataBeanList.get(position).getUrl().contains(Constant.BASE_LIBRARY) && isLogin) {

                                SharedPreferences sp = mContext.getSharedPreferences(Constant.VID_CODE, Context.MODE_PRIVATE);
                                String vidGet = sp.getString("vid", "(null)");
                                bundle.putString(Constant.TO_TEST_RULE, mDataBeanList.get(position).getUrl() + vidGet);
                                bundle.putString(Constant.TO_TEST_RULE_TITLE, mDataBeanList.get(position).getTitle());
                            } else {
                                bundle.putString(Constant.TO_TEST_RULE, mDataBeanList.get(position).getUrl());
                                bundle.putString(Constant.TO_TEST_RULE_TITLE, mDataBeanList.get(position).getTitle());
                                bundle.putString(Constant.TO_TEST_RULE_LIBTYPE, mDataBeanList.get(position).getLibtype());
                            }
                            intent.putExtras(bundle);
                            mContext.startActivity(intent);

//                                mMainAdapterInterface.testRule();

//                            } else if (mDataBeanList.get(position).getUrl().equals("http://chushihongbao")) {
//                                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
//                                dataEntity.readInfoFromLocal(mContext);
//                                if (!TextUtils.isEmpty(dataEntity.getUserid())) {
//                                    mMainAdapterInterface.getFirstReward();
//                                } else {
//                                    Intent intent = new Intent(mContext, LoginAcivity.class);
//                                    mContext.startActivity(intent);
//                                }
//                            } else if (mDataBeanList.get(position).getUrl().equals("http://fushi")) {
//                                UserBean.DataEntity dataEntity = new UserBean.DataEntity();
//                                dataEntity.readInfoFromLocal(mContext);
//                                mMainAdapterInterface.getSecondTest();
//                            }

//                            String url = mDataBeanList.get(position).getUrl();
//
//                            String title = mDataBeanList.get(position).getTitle();
//                            Intent intent = new Intent(mContext, WebActivity.class);
//                            Bundle bundle = new Bundle();
//                            bundle.putString("url",url);
//
//                            bundle.putString("title",title);
//                            intent.putExtras(bundle);
//                            mContext.startActivity(intent);

                        }
                    }
                });
                break;
            }
            case 1: {
                ViewHolder1 holder1 = (ViewHolder1) holder;
                holder1.mMainBtnTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UserBean.DataEntity dataEntity = new UserBean.DataEntity();
                        dataEntity.readInfoFromLocal(mContext);
                        if (TextUtils.isEmpty(dataEntity.getUserid())) {
                            Intent intent = new Intent(mContext, LoginAcivity.class);
                            intent.putExtra(Constant.ACTIVITY_TO_LOGIN, 100);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, ReadyTestActivity.class);
                            mContext.startActivity(intent);
                        }
                    }
                });
                holder1.mBtnToRank.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, RankActivity.class);
                        mContext.startActivity(intent);
                    }
                });
                holder1.mBtnToExamNote.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initUserid();
                        if (userid != null && !userid.isEmpty()) {
                            Intent intent = new Intent(mContext, MyTestNoteActivity.class);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, LoginAcivity.class);
                            intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.IS_MAIN_TO_LOGIN);
                            mContext.startActivity(intent);
                        }
                    }
                });
                holder1.mBtnLinkTest.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMainAdapterInterface.btnLinkTest();
                    }
                });
//                holder1.mBtnRandomTrain.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent = new Intent(mContext, TrainActivity.class);
//                        intent.putExtra(Constant.TRAIN, Constant.TRAIN_RANDOM);
//                        mContext.startActivity(intent);
//                    }
//                });
                holder1.mBtnDiffTrain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initUserid();
                        if (userid == null || userid.isEmpty()) {
                            Intent intent = new Intent(mContext, LoginAcivity.class);
                            intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.IS_MAIN_TO_LOGIN);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, ExerciseActivity.class);
                            intent.putExtra(Constant.TRAIN, Constant.TRAIN_ERROR);
                            mContext.startActivity(intent);
                        }
                    }
                });
                holder1.mBtnWrongTrain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initUserid();
                        if (userid == null || userid.isEmpty()) {
                            Intent intent = new Intent(mContext, LoginAcivity.class);
                            intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.IS_MAIN_TO_LOGIN);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, ExerciseActivity.class);
                            intent.putExtra(Constant.TRAIN, Constant.TRAIN_ERROR);
                            mContext.startActivity(intent);
                        }
                    }
                });
                holder1.mBtnCount.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, CountActivity.class);
                        intent.putExtra(Constant.MAIN_TO_COUNT, Constant.IS_MAIN_TO_COUNT);
                        mContext.startActivity(intent);
                    }
                });
                holder1.mBtnCollectTrain.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initUserid();
                        if (userid == null || userid.isEmpty()) {
                            Intent intent = new Intent(mContext, LoginAcivity.class);
                            intent.putExtra(Constant.ACTIVITY_TO_LOGIN, Constant.IS_MAIN_TO_LOGIN);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, ExerciseActivity.class);
                            intent.putExtra(Constant.TRAIN, Constant.COLLECT);
                            mContext.startActivity(intent);
                        }
                    }
                });
                break;
            }
            case 2: {
//                holder3.mBtnSkill.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent = new Intent(mContext, QuestionListActivity.class);
//                        mContext.startActivity(intent);
//                    }
//                });
                ViewHolderForum forum = (ViewHolderForum) holder;
                forum.mVpForum.setAdapter(new MainForum2Adapter(mContext, mList));
                forum.mTvBbs.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMainAdapterInterface.clickToBBS();
                    }
                });

                forum.mTvAnswer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, QuestionListActivity.class);
                        mContext.startActivity(intent);
                    }
                });

                forum.mVpForum.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        mMainAdapterInterface.itemClick(mList.get(position).getId());
                    }
                });

                switch (mList.size()) {
                    case 0: {
                        forum.mVpForum.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT, 0));
                        break;
                    }
                    case 1: {
                        forum.mVpForum.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 150)));
                        break;
                    }
                    case 2: {
                        forum.mVpForum.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 300)));
                        break;
                    }
                    case 3: {
                        forum.mVpForum.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 450)));
                        break;
                    }
                }

                break;
            }
            case 3: {
                ViewHolderInfo info = (ViewHolderInfo) holder;
//                info.mVpInfo.setAdapter(new MainInfoPagerAdapter(manager));
                info.mLvInfo.setAdapter(new MainInfoAdapter(mContext, getIHome));

                info.mLvInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(mContext, InfoDetailActivity.class);
                        intent.putExtra(Constant.INFO_DETAIL, getIHome.get(position).getId());
                        intent.putExtra(Constant.INFO_TITLE, getIHome.get(position).getTitle());
                        mContext.startActivity(intent);
                    }
                });

                switch (getIHome.size()) {
                    case 0: {
                        info.mLvInfo.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        0));
                        break;
                    }
                    case 1: {
                        info.mLvInfo.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 100)));
                        break;
                    }
                    case 2: {
                        info.mLvInfo.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 200)));
                        break;
                    }
                    case 3: {
                        info.mLvInfo.setLayoutParams(
                                new LinearLayout.LayoutParams(
                                        ViewGroup.LayoutParams.MATCH_PARENT,
                                        DeviceUtil.dip2px(mContext, 300)));
                        break;
                    }
                }

                break;
            }
            case 4: {
                ViewHolder2 holder2 = (ViewHolder2) holder;
                holder2.mMainShareWxTimeline.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMainAdapterInterface.shareToWechatTimeLine();
                    }
                });
                holder2.mMainShareWxFriend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMainAdapterInterface.shareToWechatFriend();
//                        WXUtil.WXShareToFriend("http://www.chinayarn.com/app/dxt.html", mContext);
                    }
                });
                break;
            }
        }
    }

    private void initDots(ViewHolder0 holder0) {
        dotList = new ArrayList<>();
        unselect = new LinearLayout.LayoutParams(DeviceUtil.dip2px(mContext, 5), DeviceUtil.dip2px(mContext, 5));
        unselect.leftMargin = DeviceUtil.dip2px(mContext, 6);
        unselect.gravity = Gravity.BOTTOM;

        selectParams = new LinearLayout.LayoutParams(DeviceUtil.dip2px(mContext, 7), DeviceUtil.dip2px(mContext, 7));
        selectParams.leftMargin = DeviceUtil.dip2px(mContext, 6);
        selectParams.gravity = Gravity.BOTTOM;

        for (int i = 0; i < bannerUri.size() && i <= 10; i++) {
            dots = new ImageView(mContext);
            dots.setLayoutParams(unselect);
            dots.setBackgroundResource(R.drawable.bg_dots);
            holder0.mMainDot.addView(dots);
            dotList.add(dots);
        }
    }

    private void initSelectDot(int select) {
        for (int i = 0; i < dotList.size(); i++) {
            ImageView img = dotList.get(i);
            if (i == select) {
                img.setLayoutParams(selectParams);
                img.setImageResource(R.drawable.bg_dots_select);
            } else {
                img.setLayoutParams(unselect);
                img.setImageResource(R.drawable.bg_dots);
            }
        }
    }

    public void setMainAdapterInterface(MainAdapterInterface mainAdapterInterface) {
        mMainAdapterInterface = mainAdapterInterface;
    }

    public void startTurn() {
        initDots(holder0);
        holder0.mMainBanner.notifyDataSetChanged();
        holder0.mMainBanner.startTurning(10000);
    }

    static class ViewHolder0 extends RecyclerView.ViewHolder {
        @Bind(R.id.main_banner)
        ConvenientBanner mMainBanner;
        @Bind(R.id.img_title)
        TextView mImgTitle;
        @Bind(R.id.main_dot)
        LinearLayout mMainDot;

        ViewHolder0(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

//    static class ViewHolder3 extends RecyclerView.ViewHolder {
//        @Bind(R.id.btn_compay)
//        LinearLayout mBtnCompay;
//        @Bind(R.id.btn_product)
//        LinearLayout mBtnProduct;
//        @Bind(R.id.btn_skill)
//        LinearLayout mBtnSkill;
//        @Bind(R.id.btn_professior)
//        LinearLayout mBtnProfessior;
//
//        ViewHolder3(View view) {
//            super(view);
//            ButterKnife.bind(this, view);
//        }
//    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.main_btn_test)
        RelativeLayout mMainBtnTest;
        @Bind(R.id.btn_to_rank)
        LinearLayout mBtnToRank;
        @Bind(R.id.btn_to_exam_note)
        LinearLayout mBtnToExamNote;
        @Bind(R.id.btn_link_test)
        LinearLayout mBtnLinkTest;
        @Bind(R.id.btn_random_train)
        LinearLayout mBtnRandomTrain;
        @Bind(R.id.btn_diff_train)
        LinearLayout mBtnDiffTrain;
        @Bind(R.id.btn_wrong_train)
        LinearLayout mBtnWrongTrain;
        @Bind(R.id.btn_my_quiz)
        View mBtnMyQuiz;
        @Bind(R.id.btn_count)
        LinearLayout mBtnCount;
        @Bind(R.id.btn_my_answer)
        View mBtnMyAnswer;
        @Bind(R.id.btn_collect_train)
        LinearLayout mBtnCollectTrain;
        @Bind(R.id.btn_compay)
        LinearLayout mBtnCompay;
        @Bind(R.id.btn_product)
        LinearLayout mBtnProduct;
        @Bind(R.id.btn_skill)
        LinearLayout mBtnSkill;
        @Bind(R.id.btn_professior)
        LinearLayout mBtnProfessior;
        @Bind(R.id.swipeListview)
        SwipeListView mSwipeListview;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @Bind(R.id.main_share_wx_friend)
        LinearLayout mMainShareWxFriend;
        @Bind(R.id.main_share_wx_timeline)
        LinearLayout mMainShareWxTimeline;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolderForum extends RecyclerView.ViewHolder {
        @Bind(R.id.line_blue)
        ImageView mLineBlue;
        @Bind(R.id.tv_answer)
        TextView mTvAnswer;
        @Bind(R.id.tv_bbs)
        TextView mTvBbs;
        @Bind(R.id.lv_qa)
        ListView mVpForum;

        ViewHolderForum(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static class ViewHolderInfo extends RecyclerView.ViewHolder {
        @Bind(R.id.line_blue)
        ImageView mLineBlue;
        @Bind(R.id.lv_info)
        ListView mLvInfo;

        ViewHolderInfo(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
